home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 May / may_2001.iso / intercd / root / Multimedia / ^DivX_Article / virtualdub / VirtualDub-source-1_4d / f_averag.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-20  |  6.4 KB  |  160 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    Copyright (C) 1998-2001 Avery Lee
  3. //
  4. //    This program is free software; you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation; either version 2 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program; if not, write to the Free Software
  16. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include "filter.h"
  19.  
  20. #define READSRC(byteoff, lineoff) (*(unsigned long *)((char *)src + pitch*(lineoff) + (byteoff*4)))
  21.  
  22. #define USE_ASM
  23.  
  24. extern "C" void asm_average_run(
  25.         void *dst,
  26.         void *src,
  27.         unsigned long width,
  28.         unsigned long height,
  29.         unsigned long srcstride,
  30.         unsigned long dststride);
  31.  
  32. static int avg_run(const FilterActivation *fa, const FilterFunctions *ff) {
  33.     unsigned long w,h;
  34.     unsigned long *src = (unsigned long *)fa->src.data, *dst = (unsigned long *)fa->dst.data;
  35.     unsigned long pitch = fa->src.pitch;
  36.     unsigned long rb, g;
  37.  
  38.     src -= pitch>>2;
  39.  
  40.     rb    =   ((READSRC( 0,1)&0xff00ff) + (READSRC( 0,2)&0xff00ff)
  41.             +(READSRC( 1,1)&0xff00ff) + (READSRC( 1,2)&0xff00ff));
  42.     g    =   ((READSRC( 0,1)&0x00ff00) + (READSRC( 0,2)&0x00ff00)
  43.             +(READSRC( 1,1)&0x00ff00) + (READSRC( 1,2)&0x00ff00));
  44.     *dst ++ = ((rb/9) & 0x00ff0000) | ((rb & 0x0000ffff)/9) | ((g/9) & 0x00ff00);
  45.     ++src;
  46.  
  47.     w = fa->src.w-2;
  48.     do {
  49.         rb    =   ((READSRC(-1,1)&0xff00ff) + (READSRC(-1,2)&0xff00ff)
  50.                 +(READSRC( 0,1)&0xff00ff) + (READSRC( 0,2)&0xff00ff)
  51.                 +(READSRC( 1,1)&0xff00ff) + (READSRC( 1,2)&0xff00ff));
  52.         g    =   ((READSRC(-1,1)&0x00ff00) + (READSRC(-1,2)&0x00ff00)
  53.                 +(READSRC( 0,1)&0x00ff00) + (READSRC( 0,2)&0x00ff00)
  54.                 +(READSRC( 1,1)&0x00ff00) + (READSRC( 1,2)&0x00ff00));
  55.         *dst ++ = ((rb/9) & 0x00ff0000) | ((rb & 0x0000ffff)/9) | ((g/9) & 0x00ff00);
  56.         ++src;
  57.     } while(--w);
  58.  
  59.     rb    =   ((READSRC( 0,1)&0xff00ff) + (READSRC( 0,2)&0xff00ff)
  60.             +(READSRC(-1,1)&0xff00ff) + (READSRC(-1,2)&0xff00ff));
  61.     g    =   ((READSRC( 0,1)&0x00ff00) + (READSRC( 0,2)&0x00ff00)
  62.             +(READSRC(-1,1)&0x00ff00) + (READSRC(-1,2)&0x00ff00));
  63.     *dst ++ = ((rb/9) & 0x00ff0000) | ((rb & 0x0000ffff)/9) | ((g/9) & 0x00ff00);
  64.     ++src;
  65.  
  66.     src += fa->src.modulo>>2;
  67.     dst += fa->dst.modulo>>2;
  68.  
  69. #ifdef USE_ASM
  70.     asm_average_run(dst+1, src+1, fa->src.w-2, fa->src.h-2, fa->src.pitch, fa->dst.pitch);
  71. #endif
  72.  
  73.     h = fa->src.h-2;
  74.     do {
  75.         rb    =   ((READSRC( 0,0)&0xff00ff) + (READSRC( 0,1)&0xff00ff) + (READSRC( 0,2)&0xff00ff)
  76.                 +(READSRC( 1,0)&0xff00ff) + (READSRC( 1,1)&0xff00ff) + (READSRC( 1,2)&0xff00ff));
  77.         g    =   ((READSRC( 0,0)&0x00ff00) + (READSRC( 0,1)&0x00ff00) + (READSRC( 0,2)&0x00ff00)
  78.                 +(READSRC( 1,0)&0x00ff00) + (READSRC( 1,1)&0x00ff00) + (READSRC( 1,2)&0x00ff00));
  79.         *dst ++ = ((rb/9) & 0x00ff0000) | ((rb & 0x0000ffff)/9) | ((g/9) & 0x00ff00);
  80.         ++src;
  81. #ifndef USE_ASM
  82.         w = fa->src.w-2;
  83.         do {
  84. #if 0
  85.             rb    =   ((READSRC(-1,0)&0xff00ff) + (READSRC(-1,1)&0xff00ff) + (READSRC(-1,2)&0xff00ff)
  86.                     +(READSRC( 0,0)&0xff00ff) + (READSRC( 0,1)&0xff00ff) + (READSRC( 0,2)&0xff00ff)
  87.                     +(READSRC( 1,0)&0xff00ff) + (READSRC( 1,1)&0xff00ff) + (READSRC( 1,2)&0xff00ff));
  88.             g    =   ((READSRC(-1,0)&0x00ff00) + (READSRC(-1,1)&0x00ff00) + (READSRC(-1,2)&0x00ff00)
  89.                     +(READSRC( 0,0)&0x00ff00) + (READSRC( 0,1)&0x00ff00) + (READSRC( 0,2)&0x00ff00)
  90.                     +(READSRC( 1,0)&0x00ff00) + (READSRC( 1,1)&0x00ff00) + (READSRC( 1,2)&0x00ff00));
  91.             *dst ++ = ((rb/9) & 0x00ff0000) | ((rb & 0x0000ffff)/9) | ((g/9) & 0x00ff00);
  92. #else
  93.             rb    =   ((READSRC(-1,0)&0xff00ff) + (READSRC(-1,1)&0xff00ff) + (READSRC(-1,2)&0xff00ff)
  94.                     +(READSRC( 0,0)&0xff00ff)                            + (READSRC( 0,2)&0xff00ff)
  95.                     +(READSRC( 1,0)&0xff00ff) + (READSRC( 1,1)&0xff00ff) + (READSRC( 1,2)&0xff00ff));
  96.             g    =   ((READSRC(-1,0)&0x00ff00) + (READSRC(-1,1)&0x00ff00) + (READSRC(-1,2)&0x00ff00)
  97.                     +(READSRC( 0,0)&0x00ff00)                            + (READSRC( 0,2)&0x00ff00)
  98.                     +(READSRC( 1,0)&0x00ff00) + (READSRC( 1,1)&0x00ff00) + (READSRC( 1,2)&0x00ff00));
  99.  
  100.             rb = rb*28 + (src[pitch>>2]&0xff00ff)*32;
  101.             g = g*28 + (src[pitch>>2]&0x00ff00)*32;
  102.  
  103.             *dst ++ = ((rb & 0xff00ff00) | (g & 0x00ff0000)) >> 8;
  104. #endif
  105.             ++src;
  106.         } while(--w);
  107. #else
  108.         src += fa->src.w-2;
  109.         dst += fa->dst.w-2;
  110. #endif
  111.  
  112.         rb    =   ((READSRC( 0,0)&0xff00ff) + (READSRC( 0,1)&0xff00ff) + (READSRC( 0,2)&0xff00ff)
  113.                 +(READSRC(-1,0)&0xff00ff) + (READSRC(-1,1)&0xff00ff) + (READSRC(-1,2)&0xff00ff));
  114.         g    =   ((READSRC( 0,0)&0x00ff00) + (READSRC( 0,1)&0x00ff00) + (READSRC( 0,2)&0x00ff00)
  115.                 +(READSRC(-1,0)&0x00ff00) + (READSRC(-1,1)&0x00ff00) + (READSRC(-1,2)&0x00ff00));
  116.         *dst ++ = ((rb/9) & 0x00ff0000) | ((rb & 0x0000ffff)/9) | ((g/9) & 0x00ff00);
  117.         ++src;
  118.  
  119.         src += fa->src.modulo>>2;
  120.         dst += fa->dst.modulo>>2;
  121.     } while(--h);
  122.  
  123.     rb    =   ((READSRC( 0,0)&0xff00ff) + (READSRC( 0,1)&0xff00ff)
  124.             +(READSRC( 1,0)&0xff00ff) + (READSRC( 1,1)&0xff00ff));
  125.     g    =   ((READSRC( 0,0)&0x00ff00) + (READSRC( 0,1)&0x00ff00)
  126.             +(READSRC( 1,0)&0x00ff00) + (READSRC( 1,1)&0x00ff00));
  127.     *dst ++ = ((rb/9) & 0x00ff0000) | ((rb & 0x0000ffff)/9) | ((g/9) & 0x00ff00);
  128.     ++src;
  129.  
  130.     w = fa->src.w-2;
  131.     do {
  132.         rb    =   ((READSRC(-1,0)&0xff00ff) + (READSRC(-1,1)&0xff00ff)
  133.                 +(READSRC( 0,0)&0xff00ff) + (READSRC( 0,1)&0xff00ff)
  134.                 +(READSRC( 1,0)&0xff00ff) + (READSRC( 1,1)&0xff00ff));
  135.         g    =   ((READSRC(-1,0)&0x00ff00) + (READSRC(-1,1)&0x00ff00)
  136.                 +(READSRC( 0,0)&0x00ff00) + (READSRC( 0,1)&0x00ff00)
  137.                 +(READSRC( 1,0)&0x00ff00) + (READSRC( 1,1)&0x00ff00));
  138.         *dst ++ = ((rb/9) & 0x00ff0000) | ((rb & 0x0000ffff)/9) | ((g/9) & 0x00ff00);
  139.         ++src;
  140.     } while(--w);
  141.  
  142.     rb    =   ((READSRC( 0,0)&0xff00ff) + (READSRC( 0,1)&0xff00ff)
  143.             +(READSRC(-1,0)&0xff00ff) + (READSRC(-1,1)&0xff00ff));
  144.     g    =   ((READSRC( 0,0)&0x00ff00) + (READSRC( 0,1)&0x00ff00)
  145.             +(READSRC(-1,0)&0x00ff00) + (READSRC(-1,1)&0x00ff00));
  146.     *dst ++ = ((rb/9) & 0x00ff0000) | ((rb & 0x0000ffff)/9) | ((g/9) & 0x00ff00);
  147.     ++src;
  148.  
  149.     return 0;
  150. }
  151.  
  152. FilterDefinition filterDef_average={
  153.     0,0,NULL,
  154.     "3x3 average",
  155.     "Replaces each pixel with the 3x3 average of its neighbors.\n\n[Assembly optimized] [MMX optimized]",
  156.     NULL,NULL,
  157.     0,
  158.     NULL,NULL,
  159.     avg_run
  160. };